
//返回提交json
function getJson(formid){
    var parentfield=$("[formflag='P"+formid+"'][name]");
    var parentfieldnonull=$("[formflag='P"+formid+"'][name]:not('[nullable]')");
    var childfield=$("[formflag='C"+formid+"'][name]");
    var childfieldnonull=$("[formflag='C"+formid+"'][name]:not('[nullable]')");
    var parent="";
    var child="";
    if(parentfieldnonull.length>0 || childfieldnonull.length>0){
        if(isFieldNull(parentfieldnonull) || isFieldNull(childfieldnonull)){
            //$.msgbox.show({message:"表单填写不完整!",icon:"warn"});
            return "";
        }else {
            if(parentfield.length>0){
                for(var i=0;i<parentfield.length;i++){
                     parent+='"'+parentfield[i].name+'":"'+parentfield[i].value+'"'+((i+1)==parentfield.length?'':',');
                }
            }
            if(childfield.length>0){
                for(var i=0;i<childfield.length;i++){
                     child+='"'+childfield[i].name+'":"'+childfield[i].value+'"'+((i+1)==childfield.length?'':',');
                }
            }
            if(parent!="" || child!=""){
                return '{"parentfield":{'+parent+'},"childfield":{'+child+'}}';
            }
        }
    }
}
//验证是否为空
function isFieldNull(obj){
    var b=false;
    //console.log("长度："+obj.length);
    for(var i=0;i<obj.length;i++){
        //console.log(obj[i].name+"="+obj[i].value+",parent="+$(obj[i]).parent().html());
        if(obj[i].value==null || obj[i].value==""){
            b=true;
            $(obj[i]).parent().css({background:"#F99"});
        }else{
            $(obj[i]).parent().css({background:"#FFF"});
        }
    }
    return b;
}
function reLoginDlg(c){
	$("#dialog-confirm").html("<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>"+c+"</p>");
   $( "#dialog-confirm" ).dialog({
	      resizable: false,
	      height:200,
	      width:400,
	      modal: true,
	      title:"重新登录",
	      dialogClass: "alert",
	      buttons: {
	        "重新登录": function() {
	   			window.location.href="login.jsp";
	        },
	        "否": function() {
	          $( this ).dialog( "close" );
	        }
	      },
	      open:function(event,ui){
                $(".ui-dialog-titlebar-close", $(this).parent()).hide();
          },
          close: function(event, ui) { 
        	  $(this).dialog("destroy"); // 关闭时销毁 
          } 
   });
}
function warnDlg(c){
	$("#dialog-confirm").html("<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>"+c+"</p>");
   $( "#dialog-confirm" ).dialog({
	      resizable: false,
	      height:200,
	      width:400,
	      modal: true,
	      title:"提示信息",
	      dialogClass: "alert",
	      buttons: {
	        "关闭": function() {
	          $( this ).dialog( "close" );
	        }
	      },
	      open:function(event,ui){
                $(".ui-dialog-titlebar-close", $(this).parent()).hide();
          },
          close: function(event, ui) { 
        	  $(this).dialog("destroy"); // 关闭时销毁 
          } 
   });
}
function okDlg(c){
	$("#dialog-confirm").html("<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>"+c+"</p>");
   $( "#dialog-confirm" ).dialog({
	      resizable: false,
	      height:200,
	      width:400,
	      modal: true,
	      title:"提示信息",
	      dialogClass: "alert",
	      buttons: {
	        "关闭": function() {
	          $( this ).dialog( "close" );
	        }
	      },
	      open:function(event,ui){
                $(".ui-dialog-titlebar-close", $(this).parent()).hide();
          },
          close: function(event, ui) { 
        	  $(this).dialog("destroy"); // 关闭时销毁 
          } 
   });
}
function doSubmit(serialNum,step) {
    var d = getJson(serialNum+step);
    if (d == "") {
    	warnDlg("表单填写不完整!");
        return;
    }
    //弹出确认对话框
	$("#dialog-confirm").html("<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>确定您是否要提交该表单?</p>");
	   $( "#dialog-confirm" ).dialog({
		      resizable: false,
		      height:200,
		      width:400,
		      modal: true,
		      title:"提示信息",
		      dialogClass: "alert",
		      buttons: {
		        "确定": function() {
		   			submitaction(serialNum,step,d);
		        },
		        "取消": function() {
		          $( this ).dialog( "close" );
		        }
		      },
		      open:function(event,ui){
	                $(".ui-dialog-titlebar-close", $(this).parent()).hide();
	          },
	          close: function(event, ui) { 
	        	  $(this).dialog("destroy"); // 关闭时销毁 
	          } 
	   });
    //if(isTrue()){return;}
//    $.ajax({
//        type: "post",
//        url: "TaskAction!saveTaskFormDetail.action",
//        async: false,
//        dataType: "json",
//        data: {
//            "serialNum": serialNum,
//            "step": step,
//            "submitFormValue": d
//        },
//        success: function(e) {
//            if (e.success) {
//                okDlg(e.reason);
//                $('#rightbar').load(urlTmp);
//            } else {
//            	if(e.auth=="false"){
//            		reLoginDlg(e.reason);
//            	}else{
//            		warnDlg(e.reason);
//            	}
//            }
//        }
//    });
}

function submitaction(serialNum,step,content){
    $.ajax({
        type: "post",
        url: "TaskAction!saveTaskFormDetail.action",
        async: false,
        dataType: "json",
        data: {
            "serialNum": serialNum,
            "step": step,
            "submitFormValue": content
        },
        success: function(e) {
            if (e.success) {
                okDlg(e.reason);
                $('#rightbar').load(urlTmp);
            } else {
            	if(e.auth=="false"){
            		reLoginDlg(e.reason);
            	}else{
            		warnDlg(e.reason);
            	}
            }
        }
    })
	
}